<?php
/**
 *
 * @revision:   $Id: helper.php 1.0.0 2011-05-11 17:30:20 ALEXEY GORDEYEV IK $
 * @package	    Joomla!
 * @since       {SINCE}
 * @subpackage	AG 3D Carousel
 * @version     {PROJECTVERSION}
 * @desc        Image Carousel module based on ImageFlow object-oriented script.
 * @copyright	Copyright © 2010-{PROJECTYEAR} Alexey Gordeyev IK. All rights reserved.
 * @license		GNU General Public License v2.0
 * @author		ALEXEY GORDEYEV IK
 * @author mail	admin@gordejev.lv
 * @website		www.gordejev.lv
 *
 * CODE GENERATED BY: ALEXEY GORDEYEV IK CODE GENERATOR
 * HTTP://WWW.AGJOOMLA.COM/
 *
 *
 * The class methods
 * -------------------------------
 * getJoomlaItems($params)
 * getK2Items($params)
 * getVODItems($params)
 * fetchHTMLSnippet($item)
 * fetchHTMLCarousel($items)
 * makeImageFlowObject($params)
 * makeCarousel($params)
 *
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

define('PATH_TO_MOD', JURI::base()."modules/".$modulename);
define('PATH_TO_CSS', PATH_TO_MOD."/css/");
define('PATH_TO_JSC', PATH_TO_MOD."/scripts/");
define('PATH_TO_REF', PATH_TO_MOD."/tools/");
define('MOD_VERSION', '{PROJECTVERSION}');

/**
 * ModAGCarousel Helper
 *
 * @package		Joomla!
 * @since 		{SINCE}
 */

class ModAGCarouselHelper
{

   /**
    * Do something includeScripts method
    *
    * @param 	array
    * @return  void
    */
   protected static function includeScripts($params)
   {
      $image_flow_jsc = 'imageflow.packed.js';
      JHTML::script($image_flow_jsc, PATH_TO_JSC);
   } //end includeScripts

   /**
    * Do something includeCSS method
    *
    * @param 	array
    * @return  void
    */
   protected static function includeCSS($params)
   {
      $style = ($params->get('css_style'))?$params->get('css_style'):0;
      $styles_css     = array('default.css','dark.css');
      $image_flow_css = 'imageflow.packed.css';
      // $image_flow_css = 'imageflow.custom.css';
      JHTML::stylesheet($styles_css[$style], PATH_TO_CSS);
      JHTML::stylesheet($image_flow_css, PATH_TO_CSS);
   } //end includeCSS


   /**
    * Do something getJoomlaItems method
    *
    * @param 	array
    * @return  mixed
    */
   public static function getJoomlaItems($params)
   {

       
       
   } //end getJoomlaItems

   /**
    * Do something getK2Items method
    *
    * @param 	array
    * @return  mixed
    */
   public static function getK2Items($params)
   {

       
       
   } //end getK2Items

   /**
    * Do something getVODItems method
    *
    * @param 	array
    * @return  mixed
    */
   public static function getVODItems($params)
   {
      // Get a database object
      $db =& JFactory::getDBO();
       
      $where = array();
      $where[] = " ob.published = 1 ";
      $vod_categories = count($params->get('vod_categories'));

      if(is_array($params->get('vod_categories')) && $vod_categories > 0) {
         $where[] = ' oc.category_id IN('. implode("," , $params->get('vod_categories')) . ') ';
      } elseif($params->get('vod_categories') > 0) {
         $where[] = ' oc.category_id = '. $params->get('vod_categories');
      }

      $where = ( count($where) ? " WHERE ". implode( " AND ", $where ) : " " );
       
      $order = " ORDER BY ob.on_air_datetime DESC ";

      $count_items = ($params->get('count_items') > 0)?$params->get('count_items'):10;
      $limit = " LIMIT ".$count_items;
      $group = " GROUP BY ob.id ";
       
      $query = "SELECT ob.id, ob.name AS alt "
      . "FROM #__ag_vod_objects AS ob "
      . "LEFT JOIN #__ag_vod_objects_category AS oc ON oc.item_id = ob.id "
      . $where
      . $group
      . $order
      . $limit;
       
      $db->setQuery($query);
      $items = $db->loadObjectList();
       
      $n = count($items);
      for($i=0;$i<$n;$i++) {
         $item = &$items[$i];
         $caption_size = ($params->get('cpsize') > 0)?$params->get('cpsize'):70;
         $item->alt = self::getShortCaption($item->alt,$caption_size);
         $item->src = self::fetchVODImageUrl($item->id)."b0".rand(1,9).".jpg";
         $item->url = self::fetchVODArticleUrl($item->id);
      }
       
      return $items;
   } //end getVODItems

   /**
    * Do something fetchVODArticleUrl method
    * @access public
    * @param 	 integer
    * @return  string
    */
   public static function fetchVODArticleUrl($id)
   {
      $menu   = &JSite::getMenu();
      $Itemid = "";
      foreach($menu->_items AS $menitem) {
         if(strstr($menitem->link, '&view=allvideo&group=0') || preg_match("/allvideo(?!&)\b/",$menitem->link)) {  $Itemid = '&Itemid='.$menitem->id; }
      }
      $url = JRoute::_(JURI::base()."index.php?option=com_ag_vodmatvil&view=video&cid=".$id.$Itemid);
      return $url;
   } //end fetchVODArticleUrl

   /**
    *
    * Do something fetchVODImageUrl method
    * @access public
    * @param  integer $id
    * @return string
    */
   public static function fetchVODImageUrl($id)
   {

      jimport('joomla.application.component.helper');

      if(JComponentHelper::isEnabled('com_ag_vodmatvil', true))
      {

         $cparams               = &JComponentHelper::getParams( 'com_ag_vodmatvil' );
         $screenshots           = $cparams->get( 'screenshots' );
         $screenshot_external   = $cparams->get( 'screenshots_external' );
         $screenshot_local      = $cparams->get( 'screenshots_local' );
         $screenshot_drive      = $cparams->get( 'screenshots_drive' );

         settype($id, "string");
         $count  = strlen($id);
         if($count < 9) {
            $res = 9 - $count;
            $prefix = "";
            for($i=0;$i<$res;$i++) { $prefix .= '0';  }
            $full_id = (string) $prefix.$id;
         }

         $id_url[0] =mb_substr($full_id, 0, 3, 'UTF-8');
         $id_url[1] =mb_substr($full_id, 3, 3, 'UTF-8');
         $id_url[2] =mb_substr($full_id, 6, 3, 'UTF-8');

         if($screenshots == 0) {
            if(is_dir($screenshot_drive.':\\screenshots\\'.$id_url[0].'\\'.$id_url[1].'\\'.$id_url[2].'\\')) {
               $img_urls = $screenshot_local.$id_url[0].'/'.$id_url[1].'/'.$id_url[2]."/";
            }
            else {
               $img_urls = $screenshot_external.$id_url[0].'/'.$id_url[1].'/'.$id_url[2]."/";
            }
         }
         else {
            $img_urls = $screenshot_external.$id_url[0].'/'.$id_url[1].'/'.$id_url[2]."/";
         }
         return $img_urls;
      } else {
         return -1;
      }
   } // End fetchVODImageUrl

   /**
    *
    * Do something fetchHTMLSnippet method
    * @access public
    * @param  object $item
    * @param  array  $params
    * @return string
    */
   public static function fetchHTMLSnippet($item,$params)
   {
      $img_html = '<img src="'.$item->src.'" longdesc="'.$item->url.'" width="'.$params->get('img_width').'" height="'.$params->get('img_height').'" alt="'.$item->alt.'" />';
      return $img_html;
   } //end fetchHTMLSnippet

   /**
    *
    * Do something fetchHTMLCarousel method
    * @access public
    * @param  array $items
    * @return string
    */
   public static function fetchHTMLCarousel($items,$params)
   {
      $img_html = '<div id="'.$params->get('ImageFlowID').'" class="imageflow">';
      foreach($items as $item) {
         $img_html .= self::fetchHTMLSnippet($item, $params);
      }
      $img_html .= '</div>';
       
      return $img_html;
   } //end fetchHTMLSnippet

   /**
    * Do something makeImageFlowObject method
    *
    * @param   array
    * @return  void
    */
   protected static function makeImageFlowObject($params)
   {
      $scr_code  = "\n/* Create ImageFlow Object */\n";
      $scr_code .= "domReady(function() \n";
      $scr_code .= "{ \n";
      $scr_code .= "   var AGCarousel = new ImageFlow(); \n";
      $scr_code .= "   AGCarousel.init({  \n";
       
      $scr_params[] = "      animationSpeed:     ".$params->get('animationSpeed');
      $scr_params[] = "      aspectRatio:        ".$params->get('aspectRatio');
      $scr_params[] = "      buttons:            ".$b = ($params->get('buttons'))?'true':'false';
      $scr_params[] = "      slider:             ".$b = ($params->get('slider'))?'true':'false';
      $scr_params[] = "      sliderWidth:        ".$params->get('sliderWidth');
      $scr_params[] = "      captions:           ".$c = ($params->get('captions'))?'true':'false';
      $scr_params[] = "      circular:           ".$r = ($params->get('circular'))?'true':'false';
      $scr_params[] = "      imageCursor:        '".$params->get('imageCursor')."'";
      $scr_params[] = "      ImageFlowID:        '".$params->get('ImageFlowID')."'";
      $scr_params[] = "      imageFocusM:        ".$params->get('imageFocusM');
      $scr_params[] = "      imageFocusMax:      ".$params->get('imageFocusMax');
      $scr_params[] = "      imagesHeight:       ".$params->get('imagesHeight');
      $scr_params[] = "      percentLandscape:   ".$params->get('percentLandscape');
      $scr_params[] = "      reflectPath:        '".PATH_TO_REF."'";
      $scr_params[] = "      reflections:        ".$f = ($params->get('reflections'))?'true':'false';
      $scr_params[] = "      reflectionP:        ".$params->get('reflectionP');
      $scr_params[] = "      slideshow:          ".$h = ($params->get('slideshow'))?'true':'false';
      $scr_params[] = "      slideshowSpeed:     ".$params->get('slideshowSpeed');
      $scr_params[] = "      slideshowAutoplay:  ".$a = ($params->get('slideshowAutoplay'))?'true':'false';
      $scr_params[] = "      glideToStartID:     ".$g = ($params->get('glideToStartID'))?'true':'false';
      $scr_params[] = "      startAnimation:     ".$t = ($params->get('startAnimation'))?'true':'false';
      $scr_params[] = "      xStep:              ".$params->get('xStep');

      $scr_code .= ( count($scr_params) ? "". implode( ",\n", $scr_params ) : "\n" );
      $scr_code .= "\n   });\n";
      $scr_code .= "});\n";

      $document =& JFactory::getDocument();
      $document->addScriptDeclaration($scr_code,"text/javascript");
   } //end makeImageFlowObject

   /**
    * Do something makeCarouselStyles method
    *
    * @param   array
    * @return  void
    */
   protected static function makeCarouselStyles($params)
   {
      $css_code  = "";
      $css_code .= "#SliderImageFlow {\n";
      $bgcolor   = ($params->get('bgcolor') != '')?$params->get('bgcolor'):'transparent';
      $css_code .= "  background-color: ".$bgcolor."!important;\n";
      $css_code .= "}\n";
      $css_code .= "#SliderImageFlow .images {\n";
      $css_code .= "  background: transparent!important;\n";
      $css_code .= "}\n";
      $cpcolor   = ($params->get('cpcolor') != '')?$params->get('cpcolor'):'#000';
      $css_code .= "#SliderImageFlow_caption {\n";
      $css_code .= "  color: ".$cpcolor."!important;\n";
      $css_code .= "}\n";

      $document =& JFactory::getDocument();
      $document->addStyleDeclaration($css_code,"text/css");

   } // makeCarouselStyles

   /**
    *
    * Caption Shorter
    * @access public
    * @param  string $string
    * @param  integer $count
    * @return string
    */
   public static function getShortCaption($string,$count)
   {
      if( mb_strlen($string,'UTF-8') > $count) {
         //mb_substr
         $new_string = mb_strcut(trim($string),0,$count*2,'UTF-8')."...";
      }
      else { $new_string = $string; }

      // $new_string = htmlentities($new_string, ENT_QUOTES);

      return $new_string;
       
   }

   /**
    * Do something makeCarousel method
    *
    * @param   array
    * @return  void
    */
   public static function makeCarousel($params)
   {
      self::includeCSS($params);
      self::includeScripts($params);
      self::makeImageFlowObject($params);
      self::makeCarouselStyles($params);

       
      switch($params->get('data_source')) {
         case 1:
            $items    = self::getJoomlaItems($params);
            break;
         case 2:
            $items    = self::getK2Items($params);
            break;
         case 3:
            $items    = self::getVODItems($params);
            break;
      }

      $carousel = self::fetchHTMLCarousel($items, $params);

      return $carousel;
   } //end makeImageFlowObject

} // END ModAgcarouselHelper

?>